package com.ruoyi.client.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.client.mapper.UserMapper;
import com.ruoyi.client.pojo.form.UserLoginForm;
import com.ruoyi.client.pojo.form.UserRegisterForm;
import com.ruoyi.client.pojo.form.UserUpdateForm;
import com.ruoyi.client.pojo.form.UserUpdatePwdForm;
import com.ruoyi.client.pojo.vo.UserLoginVo;
import com.ruoyi.client.pojo.vo.UserRegisterVo;
import com.ruoyi.client.pojo.vo.UserVo;
import com.ruoyi.client.service.UserService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.myshop.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("ClientUserServiceImpl")
public class UserServiceImpl implements UserService {

    @Resource(name = "ClientUserMapper")
    private UserMapper userMapper;

    // 用户登录
    @Override
    public UserLoginVo login(UserLoginForm form) {
        User user = userMapper.selectByName(form.getName());
        if (user == null) {
            throw new ServiceException("用户名不存在");
        }

        String passwordMd5 = Md5Utils.hash(form.getPassword() + user.getSalt());
        if(passwordMd5.equals(user.getPassword())){
            UserLoginVo userLoginVo = new UserLoginVo();
            BeanUtils.copyProperties(user,userLoginVo);
            return userLoginVo;
        }
        throw new ServiceException("用户登录错误");
    }
    // 用户注册
    @Override
    public UserRegisterVo register(UserRegisterForm form) {
        User user = userMapper.selectByName(form.getName());
        if (user != null) {
            throw new ServiceException("用户名已存在");
        }
        user = new User();
        BeanUtils.copyProperties(form,user);
        // 密码加密处理
        String salt = Md5Utils.hash(String.valueOf(System.currentTimeMillis()));//当前时间戳作为加密佐料
        String passwordMd5 = Md5Utils.hash(form.getPassword() + salt); //加密
        user.setSalt(salt);
        user.setPassword(passwordMd5);
        // 调用数据层,注册用户数据
        if (userMapper.register(user)) {
            UserRegisterVo userRegisterVo = new UserRegisterVo();
            BeanUtils.copyProperties(user, userRegisterVo);
            return userRegisterVo;
        }
        throw new ServiceException("用户注册错误");
    }
    // 查询我的详情
    @Override
    public UserVo getMyDetail() {
        Long userId = StpUtil.getLoginIdAsLong(); // 获取当前登录用户的Id
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    // 更改用户信息
    @Override
    public boolean update(UserUpdateForm form) {
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();

        User user = new User();
        BeanUtils.copyBeanProp(user,form);// 赋值Bean  form -> user
        user.setPassword(null);  //这里不提供更改密码的
        return userMapper.update(user) > 0;
    }

    // 更改用户密码
    @Override
    public boolean updatePwd(UserUpdatePwdForm form) {
        // 用户登录验证,未登录会抛出例外NotLoginException
        StpUtil.checkLogin();

        //查询用户
        Long userId = form.getId();
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new ServiceException("id对应用户不存在");
        }
        // 验证原密码
        String passwordOld = form.getPasswordOld();
        String passwordOldMd5 = Md5Utils.hash(passwordOld + user.getSalt());//加密
        if (!StrUtil.equals(user.getPassword(), passwordOldMd5)) {
            throw new ServiceException("用户原密码错误");
        }
        // 加密新密码
        String passwordNew = form.getPasswordNew();
        String passwordMd5 = Md5Utils.hash(passwordNew + user.getSalt());

        // 更新密码
        User userForUpdate = new User(); // 初始属性默认为null
        userForUpdate.setId(userId);
        userForUpdate.setPassword(passwordMd5);

        return userMapper.update(userForUpdate) > 0;
    }
}
